﻿/// File: ViewletControls\groupbox.fs
/// 
/// Author: Giacomo Righetti (righetti@di.unipi.it)
///   
/// ------------------------------------------------------------
/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

namespace LightweightControls

open System
open System.Drawing
open System.Windows.Forms
open System.Windows.Forms.VisualStyles

type LightweightGroupBox() as x =
    inherit LightweightControl()

    let defaultSize = Size(200,100)

    let mutable text = ""
    let mutable foreColor = Control.DefaultForeColor
    
    do x.Size <- defaultSize

    ///        
    /// Summary:
    ///     Gets or sets the text associated with this control.
    ///
    // Returns:
    ///     The text associated with this control.
    member x.Text
        with get() = text
        and set(t:string) = 
            text <- t
            base.Invalidate()
    
    ///
    /// Summary:
    ///     Gets or sets the foreground color of the control.
    ///
    /// Returns:
    ///     The foreground System.Drawing.Color of the control. The default is the value
    ///     of the System.Windows.Forms.Control.DefaultForeColor property.
    member x.ForeColor
        with get() = foreColor
        and set(c) = 
            foreColor <- c
            base.Invalidate()
            
    override internal x.IsContainer = true
    
    override x.TabStop = false
    
    override x.OnPaint(e:PaintEventArgs) =
        let g = e.Graphics
        let state = if x.Enabled then GroupBoxState.Normal else GroupBoxState.Disabled
                
        let szf = g.MeasureString(x.Text, x.Font)
        let textHalfHeight = int (szf.Height / 2.0f)
        let newBounds = Rectangle(0, textHalfHeight, x.ClientRectangle.Width, x.ClientRectangle.Height - textHalfHeight)               
        
        let bfg = Brushes.Black
        let rightToLeft = if x.RightToLeft = RightToLeft.Inherit then x.Container.Value.RightToLeft 
                                                                 else x.RightToLeft        
        let xOffset = if x.RightToLeft = RightToLeft.No then 6 else x.ClientRectangle.Width - int szf.Width - 6
        let spt = Point(xOffset + x.Position.X, x.Position.Y)
         
        GroupBoxRenderer.DrawGroupBox(g, newBounds, state)
        TextRenderer.DrawText(g, text, x.Font, spt, foreColor, x.Container.Value.BackColor)